<?php
  use yii\helpers\Html;
  use yii\helpers\Url;
  use yii\bootstrap\ActiveForm;
  use yii\widgets\LinkPager;

  $this->title = 'distribute';
?>
<style>
	ul,li{list-style: none;padding:0;margin:0;}
	em{font-style:normal;}
	.box-footer{text-align:center;}
	.box-footer button{width:200px;}
	.staff_dep li{margin-bottom:20px;}
	.uname span{font-size:16px;}
	.uname em{margin-left:30px;font-size:16px;}
	.branch_box{overflow:hidden;}
	.branch_box .branch_name,.branch_box .job_name{width:40%;float:left;}
	.branch_box .btn_box{width:20%;float:left;}
	.branch_box .branch_name span,.branch_box .job_name span{font-size:16px;line-height:34px;}
	.branch_box .branch_name select,.branch_box .job_name select{width:180px;margin-left:20px;padding:6px 8px;outline:none;border:1px solid #d2d6de;border-radius:3px;}
	.branch_box .btn_box .del{margin-left:15px;}
</style>
<section class="content">
	<div class="row">
		<div class="col-md-12">
			<div class="box box-info">
				<div class="box-header with-border">
					<h3 class="box-title">分配部门</h3>
				</div>
				<?php $form = ActiveForm::begin(['action' => ['employees/distribute', 'id' => $id], 'method' => 'post']);?>
					<div class="box-body">
						<ul class="staff_dep">
							<li class="uname"><span>员工姓名:</span><em><?= $name?></em></li>
							<li><span class="btn btn-primary add" onclick="add()">增加</span></li>
							<?php foreach ($emp_depart as $val) :?>
								<li class="branch_box">
									<div class="branch_name"><span>部门:</span><?=Html::dropDownList('did[]', $val->did, $depart, []);?></div>
									<div class="job_name"><span>职位:</span><?=Html::dropDownList('identity[]', $val->identity, $identity, []);?></div>
									<div class="btn_box"><span class="btn btn-danger del" onclick="del(this)">删除</span></div>
								</li>
							<?php endforeach; ?>
						</ul>
					</div>
					<div class="box-footer">
						<button type="submit" class="btn btn-primary">提交</button>
					</div>
				<?php ActiveForm::end();?>
			</div>
		</div>
	</div>
</section>
<script src="js/jquery.js"></script>
<script>
	var arr1='<?= $json_depart ?>';
	var arr2='<?= $json_identity ?>';
	arr1=JSON.parse(arr1);
	arr2=JSON.parse(arr2);

	function add(){
		var branch="";
		$.each(arr1, function(key, val){
			branch+="<option value='"+key+"'>"+val+"</option>";
		});
		var job="";
		$.each(arr2, function(key, val){
			job+="<option value='"+key+"'>"+val+"</option>";
		});
		var html='<li class="branch_box"><div class="branch_name"><span>部门:</span><select name="did[]">'+branch+'</select></div>'+
		'<div class="job_name"><span>职位:</span><select name="identity[]">'+job+'</select></div>'+
		'<div class="btn_box"><span class="btn btn-danger del" onclick="del(this)">删除</span></div> </li>';
		$(".staff_dep ").append(html);
	}
	function del(ths){
		var branch=$(".branch_box");
		if(branch.length!=1){
			$(ths).parents("li").remove();
		}
	}
</script>


